package com.cheeven.cwliu.citation.steps;

import java.io.File;
import java.util.HashMap;

public class Step8 extends StepBase {

	public static void downloadCSVFile(HashMap<String, String> control) {
		logInfo("8. Download CSV Files");

		logInfo(1, "sid={0}", control.get("sid"));
		logInfo(1, "qid={0}", control.get("qid"));
		logInfo(1, "parentQid={0}", control.get("parentQid"));
		logInfo(1, "mark_to={0}", control.get("mark_to"));
		logInfo(1, "numRecsToRetrieve={0}", control.get("numRecsToRetrieve"));
		logInfo(1, "numRecords={0}", control.get("numRecords"));
		logInfo(1, "recNum={0}", control.get("recNum"));

		String textFile = httpPost("http://ets.webofknowledge.com/ETS/crsaveDataToFile.do",
				new String[][]{
					{"locale", "en_US"},
					{"fileOpt", "tabWinUnicode"},
					{"colName", "WOS"},
					{"startYear", "2009"},
					{"endYear", "2013"},
					{"action", "crsaveDataToFile"},
					{"qid", control.get("qid")},
					{"parentQid", control.get("parentQid")},
					{"sortBy", "LC.D;PY.D;LD.D;SO.A.en;VL.D;PG.A;AU.A.en"},
					{"filters", "DUMMY_VALUE"},
					{"numRecsToRetrieve", control.get("numRecsToRetrieve")},
					{"SID", control.get("sid")},
					{"product", "UA"},
					{"numRecords", control.get("numRecords")},
					{"subType", ""},
					{"recNum", control.get("recNum")},
					{"mark_to", control.get("mark_to")}});

		String documentType = control.get("documentType");
		if(documentType.startsWith("DocumentType_")) {
			documentType = documentType.substring("DocumentType_".length());
		}
		documentType = documentType.replace(" ", "_");
		
		String journalName = control.get(getAppPrefix() + ".journal.name").replace(" ", "_").toUpperCase();
		int markFrom = Integer.parseInt(control.get("markFrom"));
		int mark_from = Integer.parseInt(control.get("mark_from"));
		int mark_to = Integer.parseInt(control.get("mark_to"));
				
		String folderName = control.get(getAppPrefix() + ".save.folder");
		StringBuilder fileName = new StringBuilder("CitationReport")
				.append("-")
				.append(documentType)
				.append("-")
				.append(markFrom + mark_from - 1)
				.append("-")
				.append(markFrom + mark_from + mark_to - 2)
				.append(".csv");

		File f = saveFile(folderName, journalName, fileName.toString(), textFile);

		logInfo(1, "File saved: {0}", f);



	}
}
